package com.insigma.business.qrcode.soldier.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.insigma.business.qrcode.common.dto.SystemConfigDTO;
import com.insigma.business.qrcode.common._provider.SystemConfigProvider;
import com.insigma.business.qrcode.common.util.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class AliPayClientFactory {
    @Autowired
    private SystemConfigProvider systemConfigProvider;

    private AlipayClient client;

    public AlipayClient getInstance() {

        if (client == null) {
            synchronized (AliPayClientFactory.class) {
                if (client == null) {
                    SystemConfigDTO privateKeyDto = systemConfigProvider.getSystemConfigByName(CommonConstant.SYSTEM_CONFIG_ITEM_NAME_ALIPAY_LIFE_PRIVATE_KEY).getDto();
                    SystemConfigDTO aliPublicKeyDto = systemConfigProvider.getSystemConfigByName(CommonConstant.SYSTEM_CONFIG_ITEM_NAME_ALIPAY_LIFE_ALI_PUBLIC_KEY).getDto();
                    SystemConfigDTO appIdDto = systemConfigProvider.getSystemConfigByName(CommonConstant.SYSTEM_CONFIG_ITEM_NAME_ALIPAY_LIFE_APP_ID).getDto();
                    String appId = appIdDto.getValue();
                    String privateKey = privateKeyDto.getValue();
                    String aliPublic = aliPublicKeyDto.getValue();

                    client = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",appId,
                            privateKey,"json","GBK",aliPublic,"RSA2");
                }
            }

        }
        return client;



    }
}
